﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using AllAgreed.Models.Users;
using AllAgreed.Properties;
using WebMatrix.WebData;

namespace AllAgreed.Controllers
{
    public class UserController : Controller
    {
        private readonly AuthorizationService _authorizationService;
        private readonly IUserRepository userRepository;

        public UserController(IUserRepository userRepository, AuthorizationService authorizationService)
        {
            this.userRepository = userRepository;
            _authorizationService = authorizationService;
        }

        public ActionResult Index()
        {
            return View();
        }
        
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Save(UserDto user)
        {
            var isExistUser = userRepository.ExistUserByLogin(user.Login);
            if (isExistUser)
            {
                ModelState.AddModelError("Login",Resources.UserController_Save_Login_Fail);
                return View("Register");
            }
            user.UserIp = HttpContext.Request.UserHostAddress;
            var usr = userRepository.Save(user);
            FormsAuthentication.SetAuthCookie(usr.Login, true); // выставляем куки для авторизованных лиц
            return Redirect("/");
        }

        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        
        [HttpGet]
        [Authorize]
        public new ActionResult Profile(int? id)
        {
            if (id != null)
            {
                var user = userRepository.GetUserById((int) id);
                if (user!=null) return View("Profile",user);
            }
            var usr = userRepository.GetUserByLogin(User.Identity.Name);
            return View("Profile", usr);
        }
        

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(UserLoginDto model)
        {
            if (_authorizationService.ValidateUser(model.Login, model.Password)) 
            {
                FormsAuthentication.SetAuthCookie(model.Login, true); // выставляем куки для авторизованных лиц
                if (!String.IsNullOrEmpty(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }

                return RedirectToAction("Index","Home");
            }

            ModelState.AddModelError("", Resources.AccountController_Login_Fail);

            return View(model);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Validate(string login)
        {
            var isExistUser = userRepository.ExistUserByLogin(login);
            return Json(isExistUser);
        }

        [Authorize]
        public ActionResult All()
        {
            var users = userRepository.GetAllUsers();
            return View("Users",users);
        }

        [Authorize]
        public ActionResult Search(string query)
        {
            var users = userRepository.GetUsersByQuery(query);
            return View("Users", users);
        }
    }
}
